﻿Shader "Unlit/004"
{
   Properties
    {
        _Diffuse("Diffuse", Color) = (1,1,1,1)
        _Specular("Specular", Color) = (1,1,1,1)
        _Gloss("Gloss", Range(1,256)) = 5
        _AlphaScale("Alpha Scale", Range(0,1)) = 1
         _MainTex("MainTex",2D) = "white"{}
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" "IgnoreProjector" = "True" "RenderType"="Transparent"}
        LOD 100
        
        Pass{
            ZWrite On
            ColorMask 0
        }
       
        
        Pass
        {
            Tags{"LightMode" = "ForwardBase"}
            ZWrite Off
            Blend SrcAlpha OneMinusSrcAlpha
        
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"
            #include "Lighting.cginc"
            
            //只有在CGROGRAM内再次定义一个与属性块内名字与类型相同的变量，属性块对应的变量才能起作用
           fixed4 _Diffuse;
           fixed4 _Specular;
           float _Gloss;
           float _AlphaScale;
           sampler2D _MainTex;
           float4 _MainTex_ST;
           
           
            struct v2f
            {
                float4 vertex : SV_POSITION;
                fixed3 worldNormal : TEXCOORD0;
                float3 worldVertex : TEXCOORD1;
                float2 uv:TEXCOORD2;
            };

         
            v2f vert (appdata_full v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                //fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                //fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
                //fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
                //fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * max(0, dot(worldNormal, worldLight));
                
                //fixed3 reflectLightDir = normalize(reflect(-worldLight, worldNormal));
                //fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - UnityObjectToWorldDir(v.vertex).xyz);
                
                //fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(reflectLightDir,viewDir)),_Gloss);
               //o.color = diffuse + ambient + specular;
               o.worldVertex = mul(unity_ObjectToWorld, v.vertex);
               o.worldNormal = UnityObjectToWorldNormal(v.normal);
               o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
               
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
            
                fixed4 texColor = tex2D(_MainTex, i.uv);
                //漫反射
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                //fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
                fixed3 worldLightDir = UnityWorldSpaceLightDir(i.worldVertex);
                
                fixed3 diffuse = _LightColor0.rgb * texColor.rgb * _Diffuse.rgb * max(0, dot(i.worldNormal, worldLightDir));
                
                //高光反射
               // fixed3 reflectDir = normalize(reflect(-worldLightDir, i.worldNormal));
                //fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldVertex.xyz);
                
               // fixed3 viewDir = UnityWorldSpaceViewDir(i.worldVertex);
                
                //fixed3 specular = _LightColor0.rgb_AlphaScale * _Specular.rgb * pow(saturate(dot(reflectDir,viewDir)),_Gloss);
                
                //blinphong 高光反射 需要用到半角向量 和 法向量的点击
                //半角向量 是指光源方向和视角夹角一半的向量
                //半角向量 = 光源方向 + 视角方向 然后再归一化
                
               // fixed3 halfDir = normalize(worldLightDir + viewDir);
                //fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(halfDir,i.worldNormal)),_Gloss);
                
                //之后建议用unity的内置函数 处于UnityCG.cginc文件中
                
                
                
                
                fixed3 color = ambient + diffuse;
                return fixed4(color,texColor.a * _AlphaScale);
            }
            ENDCG
        }
    }
    FallBack "Transparent/VertexLit"
}
